package com.zoe.wan.android.example.common.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnBannerListener
import com.youth.banner.listener.OnPageChangeListener
import com.zoe.wan.android.example.R
import com.zoe.wan.android.example.databinding.ItemHomeBannerBinding
import com.zoe.wan.android.example.databinding.ItemHomeListBinding
import com.zoe.wan.android.example.repositry.data.home.HomeBannerData
import com.zoe.wan.android.example.repositry.data.home.HomeBannerDataItem
import com.zoe.wan.android.example.repositry.data.home.HomeListDataData

class HomeListAdpter : RecyclerView.Adapter<ViewHolder>() {

    companion object {
        private val BannerItemScroll = 0
        private val BannerItemNormal = 1
    }

    private var dataList: List<HomeListDataData> = mutableListOf()

    private var bannerData: HomeBannerData? = null

    private var itemListener: HomeItemCollectListener? = null

    interface HomeItemCollectListener {
        fun itemCollect(id: String, position: Int)

        fun itemClick(title: String, link: String)

        fun bannerClick(title:String, link:String)
    }

    fun registerListener(listener: HomeItemCollectListener?) {
        this.itemListener = listener
    }

    fun setData(list: List<HomeListDataData>) {
        if (list.isNotEmpty()) {
            dataList = list
            notifyDataSetChanged()
        }
    }

    fun setBannerData(data: HomeBannerData) {
        bannerData = data
        notifyDataSetChanged()

    }

    fun setCollect(position: Int) {
        dataList[position].collect = true
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        if (viewType == BannerItemNormal) {
            return HomeListViewHolder(
                DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_home_list,
                    parent,
                    false
                )
            )
        } else {
            return HomeBannerViewHolder(
                DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_home_banner,
                    parent,
                    false
                )
            )
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        if (holder is HomeListViewHolder) {
            val item = dataList[position]
            holder.binding.item = item
            if (item.collect == true) {
                holder.binding.itemHomeCollect.setBackgroundResource(R.drawable.img_collect)
            } else {
                holder.binding.itemHomeCollect.setBackgroundResource(R.drawable.img_collect_grey)
            }

            holder.binding.itemHomeCollect.setOnClickListener {
                itemListener?.let {
                    it.itemCollect("${item.id}", position)
                }
            }
            // cell点击
            holder.binding.root.setOnClickListener {
                itemListener?.itemClick(item?.title ?: "",item?.link ?: "")
            }
        } else if (holder is HomeBannerViewHolder) {

            holder.binding.itemHomeBanner.setAdapter(HomeBannerAdapter(bannerData))
                .setIndicator(CircleIndicator(holder.binding.itemHomeBanner.context))
                .addOnPageChangeListener(object : OnPageChangeListener {
                    override fun onPageScrolled(
                        position: Int,
                        positionOffset: Float,
                        positionOffsetPixels: Int
                    ) {

                    }

                    override fun onPageSelected(position: Int) {
                        val item = bannerData?.get(position)
                        item?.let {
                            holder.binding.itemHomeBannerTitle.text = it.title
                        }

                    }

                    override fun onPageScrollStateChanged(state: Int) {

                    }

                }).setOnBannerListener(object : OnBannerListener<HomeBannerDataItem> {
                    // banner点击事件
                    override fun OnBannerClick(data: HomeBannerDataItem?, position: Int) {
                        ToastUtils.showLong("${data?.title}")
                        itemListener?.bannerClick(data?.title ?: "",data?.url ?: "")
                    }

                })

        }


    }

    override fun getItemViewType(position: Int): Int {
        if (position == 0) {
            return BannerItemScroll
        } else {
            return BannerItemNormal
        }
    }


    class HomeListViewHolder(val binding: ItemHomeListBinding) :
        RecyclerView.ViewHolder(binding.root) {


    }

    class HomeBannerViewHolder(val binding: ItemHomeBannerBinding) :
        RecyclerView.ViewHolder(binding.root) {


    }

    class HomeBannerAdapter(banner: HomeBannerData?) :
        BannerImageAdapter<HomeBannerDataItem>(banner) {
        override fun onBindView(
            holder: BannerImageHolder?,
            data: HomeBannerDataItem?,
            position: Int,
            size: Int
        ) {
            holder?.imageView?.let { img ->
                Glide.with(img).load(data?.imagePath).into(img)
            }

        }


    }

}